import util.GuiImg;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Transfer extends JPanel implements ActionListener {
    private Icon atmIcon11 = GuiImg.getIcon("/imgs/buttonSure.jpg");
    private JButton btnlogin11 = new JButton(atmIcon11);
    private JTextField textField3 = new JTextField();
    private JTextField textField4 = new JTextField();
    private Atm frame;
    public Transfer(Atm frame){
        this.frame = frame;
        this.setLayout(null);
        //账号显示框
        JLabel textlabel3 = new JLabel("转账账号：");
        textlabel3.setBounds(180, 150, 100, 30);
        this.add(textlabel3);
        //单行输入框
        textField3.setBounds(250, 150, 250, 30);
        this.add(textField3);
        //金额显示框
        JLabel textlabel4 = new JLabel("转账金额：");
        textlabel4.setBounds(180, 200, 100, 30);
        this.add(textlabel4);
        //单行输入框
        textField4.setBounds(250, 200, 250, 30);
        this.add(textField4);
        //密码按钮
        this.setOpaque(false);
        btnlogin11.setBounds(330, 300, 100, 40);
        this.add(btnlogin11);
        btnlogin11.addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        for (int i=0;i<frame.getUserArray().size();i++){
            if (textField3.getText().equals(frame.getUserArray().get(i).getId())){
                if (!textField3.getText().equals(frame.getUserArray().get(frame.getIndex()).getId())){
                    if (Double.valueOf(textField4.getText())<=Double.valueOf((frame.getUserArray().get(frame.getIndex()).getMoney()))){
                        frame.getUserArray().get(i).setMoney(frame.getUserArray().get(i).getMoney()+Double.valueOf(textField4.getText()));
                        frame.getUserArray().get(frame.getIndex()).setMoney(frame.getUserArray().get(frame.getIndex()).getMoney()-Double.valueOf(textField4.getText()));
                        JOptionPane.showMessageDialog(this, "转账成功");
                        break;
                    }
                    JOptionPane.showMessageDialog(this, "余额不足");
                    break;
                }
                JOptionPane.showMessageDialog(this, "无法给自己转账");
                break;
            }
            if (i==frame.getUserArray().size()-1){
                JOptionPane.showMessageDialog(this, "查无此人");
                break;
            }
        }
        frame.switchPanel("菜单");
    }

    public JTextField getTextField3() {
        return textField3;
    }

    public void setTextField3(JTextField textField3) {
        this.textField3 = textField3;
    }

    public JTextField getTextField4() {
        return textField4;
    }

    public void setTextField4(JTextField textField4) {
        this.textField4 = textField4;
    }
}
